package cn.ctyun.videoplayer.playerevent;

import android.content.Context;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import cn.ctyun.videoplayer.player.BaseIjkVideoView;
import cn.ctyun.videoplayer.util.PlayerConstants;

/**
 * Author: liujingtao
 * Date: 2019/4/8 11:43
 * Description: 播放器事件代理事件
 */
public class PlayerEventInvocationHandler<T> implements InvocationHandler {

    T target;
    Context context;
    BaseIjkVideoView videoView;

    public PlayerEventInvocationHandler(T target) {
        this.target = target;
    }

    public PlayerEventInvocationHandler(Context context, BaseIjkVideoView videoView) {
        this.context = context;
        this.videoView = videoView;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

        if (target instanceof BaseIjkVideoView) {
            Context context = ((BaseIjkVideoView) target).getAttachActivity();
            if ("pause".equals(method.getName())) {
                PlayerEventManager.getInstance(context).savePlayerEvent(PlayerConstants.PlayerEvent.PAUSE, (BaseIjkVideoView)target);
            }
        } else {
            PlayerEventManager.getInstance(context).savePlayerEvent(PlayerConstants.PlayerEvent.PAUSE, videoView);
        }

        return method.invoke(target, objects);
    }
}
